package com.wj.ballcat.demo.controller;

import com.hccake.ballcat.common.model.result.BaseResultCode;
import com.hccake.ballcat.common.model.result.R;
import com.wj.ballcat.demo.model.dto.DemoUserDTO;
import com.wj.ballcat.demo.model.entity.DemoUser;
import com.wj.ballcat.demo.reposity.DemoUserRepository;
import com.wj.ballcat.demo.service.DemoUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/demo/user")
@Tag(name = "测试用户")
public class DemoUserController {
	private final DemoUserRepository repository;
	private final DemoUserService demoUserService;

	@GetMapping
	/**
	 * 【推荐】根据用户的名字模糊查询所有用户的详细信息，等价于上一个查询方式
	 */
	public List<DemoUser> listDemo(String name) {

		// 本框架拓展的lambda查询器lambdaQueryPlus，增加了bindOne、bindList、bindPage
		// 显然这是一种更加简便的查询方式，但是如果存在多级深度的关联关系，此种方法就不适用了，还需要借助Binder
		return repository.lambdaQueryPlus()
				.eq(name != null, DemoUser::getUsername, name)
				// 【推荐】用法一、指定属性关联，只关联身份证号这个字段。
				.bindList(DemoUser::getIdCard,DemoUser::getArticles);
//				.bindList(DemoUser::getIdCard,DemoUser::getIdCardNumber);
		// 【不推荐】用法二、全关联。
		// .bindList()
	}

	@GetMapping("/demo1")
	public List<DemoUserDTO> demoUserList(String name){
		return demoUserService.searchUserByName(name);
	}

	@GetMapping("/demo2")
	public List<DemoUserDTO> demoUserList2(String name){
		return demoUserService.searchUserByName2(name);
	}

	@PutMapping
	public R<Void> updateDemoUser(@RequestBody DemoUser demoUser){
		return repository.updateById(demoUser)?R.ok():R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "修改失败");
	}

}
